package com.py.message.controller;

import java.util.List;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;


import com.py.message.config.MyWebSocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Transient;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.py.message.domain.Message;
import com.py.message.service.IMessageService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 系统消息Controller
 * 
 * @author 童佳
 * @date 2024-12-17
 */
@RestController
@RequestMapping("/message")
public class MessageController extends BaseController
{
    @Autowired
    private IMessageService messageService;


    /**
     * 系统消息
     */
    @GetMapping("/message")
    public List<Message> selectMessage(){
        return messageService.selectMessage();
    }


    /**
     * 查询系统消息列表
     */
    @RequiresPermissions("message:message:list")
    @GetMapping("/list")
    public TableDataInfo list(Message message)
    {
        startPage();
        List<Message> list = messageService.selectMessageList(message);
        return getDataTable(list);
    }

    @GetMapping("/findList")
    public List<Message> findList(Message message)
    {
        List<Message> list = messageService.selectMessageList(message);
        return  list;
    }


    /**
     * 导出系统消息列表
     */
    @RequiresPermissions("message:message:export")
    @Log(title = "系统消息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Message message)
    {
        List<Message> list = messageService.selectMessageList(message);
        ExcelUtil<Message> util = new ExcelUtil<Message>(Message.class);
        util.exportExcel(response, list, "系统消息数据");
    }

    /**
     * 获取系统消息详细信息
     */
    @RequiresPermissions("message:message:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(messageService.selectMessageById(id));
    }

    /**
     * 新增系统消息
     */
    @RequiresPermissions("message:message:add")
    @Log(title = "系统消息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Message message)
    {
        return toAjax(messageService.insertMessage(message));
    }



    @Resource
    MyWebSocket myWebSocket;
    /**
     * 修改系统消息
     */
    @RequiresPermissions("message:message:edit")
    @Log(title = "系统消息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Message message)
    {
        myWebSocket.sendToAllClient("111");
        return toAjax(messageService.updateMessage(message));
    }

    /**
     * 删除系统消息
     */
    @RequiresPermissions("message:message:remove")
    @Log(title = "系统消息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        myWebSocket.sendToAllClient("111");
        return toAjax(messageService.deleteMessageByIds(ids));
    }
}
